#!/bin/bash
#description: install mysql or maridb server on centos7 
#定义变量
SOFTDIR="/opt/tools"
MVER=5.6.39
DATADIR=/data/m_data/
MINSTALLDIR=/usr/local
user=mysql
group=mysql
IP="$(ifconfig eth0|grep inet|grep -v inet6|awk '{print $2}')"

#初始化
function init(){
  #判断软件存放目录是否存在
  [ ! -d $SOFTDIR ] && mkdir -p $SOFTDIR
  #判断epel的yum源是否配置
  cd /etc/yum.repos.d
  [ ! -f epel-7.repo -o ! -f epel.repo ] && wget -O /etc/yum.repos.d/epel-7.repo http://mirrors.aliyun.com/repo/epel-7.repo
  #安装依赖
  yum -y install libevent-devel libaio jemalloc-devel ncurses cmake ncurses-devel gcc gcc-c++ autoconf glibc-devel libxml2 libxml2-devel bison-devel boost-devel openssl-devel	
  #关闭selinux和设置firewalld防火墙，方形3306
  sed -i "s/SELINUX=enforcing/SELINUX=disabled/g" /etc/selinux/config
  setenforce 0
  firewall-cmd --add-port=3306/tcp --permanent
  firewall-cmd --reload
  systemctl restart firewalld
}
#编译安装并配置mysql
function Compile_MySQL(){
    #判断指定版本的文件是否存在
    cd $SOFTDIR && [ ! -f mysql-5.6.39.tar.gz ] && wget https://cdn.mysql.com//Downloads/MySQL-5.6/mysql-5.6.39.tar.gz
    #判断mysql用户和数据库数据目录是否存在
    if ! id $user &>/dev/null;then
	groupadd -g 3306 mysql
        useradd -u 3306 -g mysql -r -s /sbin/nologin mysql
    fi
    [ ! -d $DATADIR ] && mkdir -p $DATADIR
    #编译安装mysql5.6.39
    cd $SOFTDIR && [ ! -d mysql-5.6.39 ] && tar xf mysql-5.6.39.tar.gz && cd mysql-5.6.39
    cmake . -DCMAKE_INSTALL_PREFIX=${MINSTALLDIR}/mysql-5.6.39 -DMYSQL_DATADIR=$DATADIR -DSYSCONFDIR=/etc -DWITH_INNOBASE_STORAGE_ENGINE=1 -DWITH_ARCHIVE_STORAGE_ENGINE=1 -DWITH_BLACKHOLE_STORAGE_ENGINE=1 -DWITH_SSL=system -DWITH_ZLIB=system -DWITH_READLINE=1 -DWITH_LOBWRAP=0 -DMYSQL-UNIX_ADDR=/tmp/mysql.sock -DDEFAULT_CHARSET=utf8mb4 -DDEFAULT_COLLATION=utf8mb4_general_ci
   if [ $? -eq 0 ];then
	make && make install
   else
	exit 1
	echo "Complie MySQL fail"
   fi
   #配置mysql
   #1)创建软链接
   cd /usr/local && [ ! -d mysql ] && ln -sv ${MINSTALLDIR}/mysql-5.6.39 ${MINSTALLDIR}/mysql
   #2)授权mysql安装目录的属主和属组，并且初始化mysql
   cd /usr/local/mysql && chown -R root.$group ./* && ./scripts/mysql_install_db --user=mysql --datadir=$DATADIR
   #3)更改mysql数据目录的属主和属组都为mysql
   chown -R $user.$group $DATADIR
   #4)编译mysql的配置文件my.cnf
   [ ! -f /etc/my.cnf ] && mv /etc/my.cnf /etc/my.cnf.ori
   cat >/etc/my.cnf <<EOF
[mysqld]
server-id = 11
port = 3306
socket=/tmp/mysql.sock
user=$user
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
datadir=$DATADIR
innodb_log_file_size = 1G
log_error = error.log
skip_name_resolve = 1
character_set_server=utf8mb4
bind_address = $IP
innodb_file_per_table = 1
slow_query_log = 1
slow_query_log_file = slow.log
log_bin = bin.log
[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid
EOF
#编写mysql的启动脚本（centos7）
#cp /usr/local//g/support-files/mysql.server /etc/init.d/mysqld 
#chmod u+x /etc/init.d/mysqld
#chkconfig --add mysqld && chkconfig mysqld on
cd /usr/lib/systemd/system
cat > mysqld.service <<EOF
[Unit]
Description=MySQL database server
After=syslog.target
After=network.target
#
[Service]
Type=simple
User=mysql
Group=mysql
ExecStart=${MINSTALLDIR}/mysql/bin/mysqld_safe --basedir=${MINSTALLDIR}/mysql --pid-file=${DATADIR}/mysqld.pid --datadir=${DATADIR}
TimeoutSec=300
#Place temp files in a secure directory,not /tmp
PrivateTmp=false
[Install]
WantedBy=multi-user.target
EOF
   #配置mysql环境变量
   echo "export PATH=$PATH:${MINSTALLDIR}/mysql/bin" >> /etc/profile.d/mysqld.sh
   source /etc/profile.d/mysqld.sh
   #启动mysql
   service mysqld start
   if [ "$(ps -ef|grep mysqld|wc -l)" -gt 2 ];then
	echo "MySQL start succfull"
   else
	echo "MySQL start fail"
   fi

   #[root@node8 mysql]# pwd
   #/usr/local//g
   #[root@node8 mysql]# ./bin/mysql_secure_installation 
}
#二进制安装并配置mysql
function Binary_MySQL(){
  #下载mysql5.6.39的二进制包
  cd $SOFTDIR && [ ! -f mysql-5.6.39-linux-glibc2.12-x86_64.tar.gz ] &&  wget https://cdn.mysql.com//Downloads/MySQL-5.6/mysql-5.6.39-linux-glibc2.12-x86_64.tar.gz
  #判断mysql用户和数据库数据目录是否存在
    if ! id $user &>/dev/null;then
        groupadd -g 3306 mysql
        useradd -u 3306 -g mysql -r -s /sbin/nologin mysql
    fi
    [ ! -d $DATADIR ] && mkdir -p $DATADIR
  #解压软件包到/usr/local目录下
  cd $SOFTDIR && tar xf mysql-5.6.39-linux-glibc2.12-x86_64.tar.gz -C /usr/local && ln -s /usr/local/mysql-5.6.39-linux-glibc2.12-x86_64 /usr/local/mysql
  #授权
  chown -R root.$group /usr/local/mysql
  chown -R root.$group /usr/local/mysql-5.6.39-linux-glibc2.12-x86_64
  #初始化并授权
  cd /usr/local/mysql && ./scripts/mysql_install_db --datadir=/data/m_data --user=mysql
  chown -R $user:$group $DATADIR
  #配置文件my.cnf
  [  -f /etc/my.cnf ] && mv /etc/my.cnf /etc/my.cnf.Compile
cat >/etc/my.cnf <<EOF
[mysqld]
server-id = 11
port = 3306
socket=/tmp/mysql.sock
user=$user
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0
datadir=$DATADIR
innodb_log_file_size = 1G
log_error = error.log
skip_name_resolve = 1
character_set_server=utf8mb4
bind_address = $IP
innodb_file_per_table = 1
slow_query_log = 1
slow_query_log_file = slow.log
log_bin = bin.log
[mysqld_safe]
log-error=${DATADIR}/mysqld.log
pid-file=${DATADIR}/mysqld.pid
EOF
   #拷贝mysql的启动脚本
   #cp /usr/local//g/support-files/mysql.server /etc/init.d/mysqld
   #chmod u+x /etc/init.d/mysqld
   #chkconfig --add mysqld && chkconfig mysqld on
 cd /usr/lib/systemd/system 
cat >mysqld.service <<EOF   
[Unit]
Description=MySQL database server
After=syslog.target
After=network.target
#
[Service]
Type=simple
User=mysql
Group=mysql
ExecStart=/usr/local/mysql/bin/mysqld_safe --basedir=/usr/local/mysql --pid-file=${DATADIR}/mysqld.pid --datadir=${DATADIR}
TimeoutSec=300
#Place temp files in a secure directory,not /tmp
PrivateTmp=false
[Install]
WantedBy=multi-user.target
EOF
   #配置mysql环境变量
   echo "export PATH=$PATH:/usr/local/mysql/bin" >> /etc/profile.d/mysqld.sh
   source /etc/profile.d/mysqld.sh
   #启动mysql
   systemctl start mysqld.service
   if [ "$(ps -ef|grep mysqld|wc -l)" -gt 2 ];then
        echo "MySQL start succfull"
   else
        echo "MySQL start fail"
   fi  
   #[root@node8 mysql]# pwd
   #/usr/local//g
   #[root@node8 mysql]# ./bin/mysql_secure_installation 

}
cat <<EOF
---------------------------------------------------------------
	install mysql server on centos7.3
--------------------------------------------------------------
|****************Please Enter your Choice:*******************|
(1) Compile and install mysql
(2) Binary package installation mysql
(3) quit
EOF
read -p "plase enter your choice:" input
case $input in
1)
  init
  Compile_MySQL
  ;;
2)
  init
  Binary_MySQL
  ;;
3)
 exit 0
esac

